<template>
  <div class="container">
    <Save ref="saveRef" @refresh="search" />
    <Answer ref="answerRef" @refresh="search"></Answer>
    <a-card
      class="general-card"
      :title="$t('menu.system.common-problem-management')"
    >
      <a-row>
        <a-col :flex="1">
          <a-form
            :model="formModel"
            :label-col-props="{ span: 6 }"
            :wrapper-col-props="{ span: 18 }"
            label-align="left"
          >
            <a-row :gutter="16">
              <a-col :span="8">
                <a-form-item field="keywords" label="关键字">
                  <a-input
                    v-model="formModel.keywords"
                    placeholder="请输入关键字查询"
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </a-col>
        <a-divider style="height: 84px" direction="vertical" />
        <a-col :flex="'86px'" style="text-align: right">
          <a-space direction="vertical" :size="18">
            <a-button type="primary" @click="search">
              <template #icon>
                <icon-search />
              </template>
              {{ $t('searchTable.form.search') }}
            </a-button>
            <a-button @click="reset">
              <template #icon>
                <icon-refresh />
              </template>
              {{ $t('searchTable.form.reset') }}
            </a-button>
          </a-space>
        </a-col>
      </a-row>
      <a-divider style="margin-top: 0" />
      <a-row style="margin-bottom: 16px">
        <a-col :span="16">
          <a-space>
            <a-button type="primary" @click="handleClickToSave()">
              <template #icon>
                <icon-plus />
              </template>
              提问
            </a-button>
          </a-space>
        </a-col>
      </a-row>
      <a-table
        row-key="id"
        :loading="loading"
        :data="renderData.rows"
        :bordered="false"
        :pagination="{
          total: renderData.total,
          pageSize: formModel.rows,
          showTotal: true,
          showPageSize: true,
        }"
        @page-change="onPageChange"
        @page-size-change="onPageSizeChange"
      >
        <template #columns>
          <a-table-column title="序号" :width="80">
            <template #cell="{ rowIndex }">
              {{ rowIndex + 1 }}
            </template>
          </a-table-column>
          <a-table-column title="问题内容" data-index="content" />
          <a-table-column title="回答" data-index="answer" />
          <a-table-column title="创建时间" data-index="createTime">
          </a-table-column>
          <a-table-column
            :title="$t('columns.operations')"
            align="center"
            data-index="operations"
            :width="250"
          >
            <template #cell="{ record }">
              <a-button
                type="text"
                size="small"
                @click="handleClickToAnswer(record.id)"
              >
                回答
              </a-button>
              <a-popconfirm
                :content="$t('operations.popConfirm.delete')"
                type="error"
                @ok="handleClickToDel(record.id)"
              >
                <a-button type="text" size="small" status="danger">
                  {{ $t('columns.operations.delete') }}
                </a-button>
              </a-popconfirm>
            </template>
          </a-table-column>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import Message from '@arco-design/web-vue/es/message';
  import useLoading from '@/hooks/loading';
  import {
    CommonProblemParams,
    CommonProblemVO,
    deleteFaq,
    getFaqList,
  } from '@/api/common-problem-management';
  import { PaginationData } from '@/types/global';
  import Answer from './components/answer.vue';
  import Save from './components/save.vue';

  const generateFormModel = () => {
    return {
      keywords: '',
      page: 1,
      rows: 20,
    };
  };
  const { loading, setLoading } = useLoading(false);
  const renderData = ref<PaginationData<CommonProblemVO>>({
    rows: [],
    total: 0,
  });
  const formModel = ref(generateFormModel());
  const saveRef = ref();
  const answerRef = ref();

  const fetchData = async (
    params: CommonProblemParams // = { current: 1, pageSize: 20 }
  ) => {
    setLoading(true);
    try {
      const { data } = await getFaqList(params);
      renderData.value = data;
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };

  const handleClickToSave = () => {
    saveRef.value.handleChangeVisible();
  };
  const handleClickToAnswer = (id: string) => {
    answerRef.value.handleChangeVisible(id);
  };
  const search = () => {
    formModel.value.page = 1;
    fetchData(formModel.value);
  };

  const handleClickToDel = (id: string) => {
    deleteFaq(id).then(() => {
      Message.success('操作成功');
      search();
    });
  };
  const onPageChange = (current: number) => {
    formModel.value.page = current;
    fetchData(formModel.value);
  };
  const onPageSizeChange = (current: number) => {
    formModel.value.rows = current;
    fetchData(formModel.value);
  };

  search();

  const reset = () => {
    formModel.value = generateFormModel();
  };
</script>

<style scoped lang="less">
  .container {
    padding: 10px;
  }

  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
  :deep(.arco-table-td-content) {
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    display: -webkit-box !important;
    -webkit-line-clamp: 3 !important;
    -webkit-box-orient: vertical !important;
  }
</style>
